package try_catch_package;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Byte_stream_demo9 {
    public static void main(String[] args) throws IOException {
        /*
         *   利用 try...catch...finally 捕获拷贝文件中代码出现的异常
         */

        //1、创建对象
        FileInputStream fis = null;
        FileOutputStream fos = null;


        try {
            fis = new FileInputStream("/Users/zhaoxiaoyong/Documents/Java/" +
                    "IO_stream_project/Byte_stream_module/src/rough_draft/a.txt");
            fos = new FileOutputStream("/Users/zhaoxiaoyong/Documents/Java/" +
                    "IO_stream_project/Byte_stream_module/src/rough_draft/b.txt");

            //2、拷贝
            int len;
            byte[] bytes = new byte[1024 * 1024 * 5];
            while ((len = fis.read(bytes)) != -1) {
                fos.write(bytes, 0, len);
            }


        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //3、释放资源
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
